package mlp.game.cherries;

import com.overkill.game.Renderable;

import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Paint.Style;
import android.graphics.RectF;

public class Box {
	
	private RectF frame;
	private int color;
	
	private Paint paintBlur;
	private Paint paintInner;
	private Paint paintInnerAlpha;
	private Paint paintStroke;
	
	private boolean active = false;
	
	public Box(RectF frame, int color) {
		this.frame = frame;
		this.color = color;
		BlurMaskFilter blurMaskFilter = new BlurMaskFilter(12, BlurMaskFilter.Blur.NORMAL);
		paintBlur = new Paint();
		paintBlur.setMaskFilter(blurMaskFilter);
		paintBlur.setColor(color);
		paintBlur.setStrokeWidth(CherryEngine.dip(3.3f));
		
		paintInner = new Paint();
		paintInner.setColor(Color.BLACK);
		paintInner.setStyle(Style.FILL);
		paintInner.setAntiAlias(true);
		
		paintInnerAlpha = new Paint();
		paintInnerAlpha.setColor(color);
		paintInnerAlpha.setStyle(Style.FILL);
		paintInnerAlpha.setAntiAlias(true);
		paintInnerAlpha.setAlpha(0);
		
		paintStroke = new Paint();
		paintStroke.setColor(color);
		paintStroke.setStyle(Style.STROKE);
		paintStroke.setStrokeWidth(CherryEngine.dip(2f));
		paintStroke.setAntiAlias(true);
	}

	public void update(long gameTime){
		int a = paintInnerAlpha.getAlpha();
		a -= 10;
		if(a < 0){
			a = 0;
		}
		paintInnerAlpha.setAlpha(a);
	}
	
	public void dropped(){
		paintInnerAlpha.setAlpha(255);
	}
	
	public void draw(Canvas canvas){
		if(isActive()){
			canvas.drawRect(frame, paintBlur);			
		}
		canvas.drawRoundRect(frame, 10, 10, paintInner);
		if(paintInnerAlpha.getAlpha() > 0)
			canvas.drawRoundRect(frame, 10, 10, paintInnerAlpha);
		canvas.drawRoundRect(frame, 10, 10, paintStroke);
	}
	
	public boolean isHit(Renderable r){
		return RectF.intersects(getFrame(), r.getFrame());
	}
	
	public boolean isHit(float x, float y){
		return getFrame().contains(x, y);
	}
	
	public boolean isHit(PointF p){
		return this.isHit(p.x, p.y);
	}
	
	public RectF getFrame() {
		return frame;
	}

	public void setFrame(RectF frame) {
		this.frame = frame;
	}

	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public boolean isActive() {
		return active;
	}
}
